Completed
Push — master ( 53b8d5...da1ee2 )
by Ajeh
28s
created

Directives.defineAlert   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
// Directives
2
3
import {noop, clickNode} from './utilities'
4
import {CONFIRM_TYPES} from './constants'
5
6
7
let Directives = function (Vue) {
8
    Object.defineProperties(this, {
9
        Vue: {get: () => Vue},
10
        confirmDefinition: {
11
            get: this.defineConfirm
12
        },
13
        alertDefinition: {
14
            get: this.defineAlert
15
        }
16
    })
17
}
18
19
Directives.prototype.defineConfirm = function () {
20
    const _this = this
21
    const DirectiveDefinition = {}
22
23
    const clickHandler = function (event, el, binding) {
24
        event.preventDefault()
25
        event.stopImmediatePropagation()
26
27
        let options = _this.getOptions(binding)
28
        let confirmMessage = _this.getConfirmMessage(binding)
29
        let thenCallback = _this.getThenCallback(binding, el)
30
        let catchCallback = _this.getCatchCallback(binding)
31
32
        _this.Vue.dialog
33
            .confirm(confirmMessage, options)
34
            .then(thenCallback)
35
            .catch(catchCallback)
36
    }
37
38
    DirectiveDefinition.bind = (el, binding) => {
39
        if (el.VuejsDialog === undefined) {
40
            el.VuejsDialog = {}
41
        }
42
43
        el.VuejsDialog.clickHandler = function (event) {
44
            clickHandler(event, el, binding)
45
        }
46
47
        el.addEventListener('click', el.VuejsDialog.clickHandler, true)
48
    }
49
50
    DirectiveDefinition.unbind = (el) => {
51
        el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
52
    }
53
54
    return DirectiveDefinition
55
}
56
57
Directives.prototype.defineAlert = function () {
58
    // Still Considering it uses case.
59
}
60
61
Directives.prototype.getConfirmMessage =  function(binding) {
62
    if (binding.value && binding.value.message) {
63
        return binding.value.message
64
    }
65
    return typeof binding.value === 'string' ? binding.value : null
66
}
67
68
Directives.prototype.getOptions =  function(binding) {
69
    let options = typeof binding.value === 'object' ? binding.value : {}
70
71
    delete options['ok']
72
    delete options['cancel']
73
74
    if(binding.arg && CONFIRM_TYPES.hasOwnProperty(binding.arg.toUpperCase())){
75
        options.type = CONFIRM_TYPES[binding.arg.toUpperCase()]
76
    }
77
78
    return options
79
}
80
81
Directives.prototype.getThenCallback =  function(binding, el){
82
    if (binding.value && binding.value.ok) {
83
        return binding.value.ok
84
    } else {
85
        return () => {
86
            // Unbind to allow original event
87
            el.removeEventListener('click', el.VuejsDialog.clickHandler, true)
88
            // Trigger original event
89
            clickNode(el)
90
            // Re-bind listener
91
            el.addEventListener('click', el.VuejsDialog.clickHandler, true)
92
        }
93
    }
94
}
95
96
Directives.prototype.getCatchCallback =  function(binding) {
97
    if (binding.value && binding.value.cancel) {
98
        return binding.value.cancel
99
    }
100
    return noop
101
}
102
103
export default Directives
104